package com.ccys.cms.service.masterData.impl;

import cn.hutool.core.util.ObjectUtil;
import com.ccys.cms.service.masterData.CountryHolidayService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.masterData.CountryHolidayDelDto;
import com.ccys.db.dto.masterData.CountryHolidayQueryPageDto;
import com.ccys.db.dto.masterData.CountryHolidaySaveDto;
import com.ccys.db.entity.TblAccount;
import com.ccys.db.entity.TblCountryHoliday;
import com.ccys.db.mapper.TblAccountCountryMapper;
import com.ccys.db.mapper.TblCountryHolidayMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.masterData.CountryHolidayQueryPageVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * @program: WhiteCoat
 * @description: 公共假日实现类
 * @author: ZhangJingCan
 * @create: 2023-12-11 13:21
 **/

@Service
public class CountryHolidayServiceImpl extends BaseServiceImpl<TblCountryHoliday> implements CountryHolidayService {

    @Resource
    private TblCountryHolidayMapper tblCountryHolidayMapper;

    /**
     * 用于保存修改状态日志
     */
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    /**
     * account 和国家的关联表
     */
    @Resource
    private TblAccountCountryMapper tblAccountCountryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult save(CountryHolidaySaveDto dto) {
        if (null != dto.getStatus()){
            baseTblAuditTrailWebService.saveStatusLog(dto,dto.getStatus());
        }
        //不校验重复 - 直接保存
        return ResponseResult.update(this.insertOrUpdate(dto));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult delById(CountryHolidayDelDto dto) {
        //判断
        if (ObjectUtil.isEmpty(dto.getId())) {
            return ResponseResult.error("Parameter Error!");
        }
        //准备实体类并赋值
        TblCountryHoliday tblCountryHoliday = new TblCountryHoliday();
        tblCountryHoliday.setId(dto.getId());
        tblCountryHoliday.setDelFlag(1);
        //判断是否修改成功 - 返回不同状态
        if (this.updateEntityById(tblCountryHoliday) > 0) {
            return ResponseResult.success("Delete Successful!");
        }
        return ResponseResult.error("Delete Failed!");
    }

    @Override
    public ResponseResult findPage(CountryHolidayQueryPageDto dto) {
        //检查并赋值
        if (ObjectUtil.isEmpty(dto.getCountryId())){
            //如果未传入国家ID
            TblAccount account = UserUtil.getCurLoginAccount();
            if (ObjectUtil.isEmpty(account)){
                return ResponseResult.error("Not Logged In");
            }
            dto.setAccountId(account.getId());
        }
        dto.setStatus(Optional.ofNullable(dto.getStatus()).filter(s -> s > -1 && s < 2).orElse(null));
        PageDto.startPage();
        List<CountryHolidayQueryPageVo> list = tblCountryHolidayMapper.findPage(dto);
        PageInfo<CountryHolidayQueryPageVo> info = new PageInfo<>(list);

        return ResponseResult.success(info);
    }
}
